package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PAttrCommonBo;
import com.ruoyi.system.domain.vo.PAttrCommonVo;
import com.ruoyi.system.domain.PAttrCommon;
import com.ruoyi.system.mapper.PAttrCommonMapper;
import com.ruoyi.system.service.IPAttrCommonService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品属性中间Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-04
 */
@RequiredArgsConstructor
@Service
public class PAttrCommonServiceImpl implements IPAttrCommonService {

    private final PAttrCommonMapper baseMapper;

    /**
     * 查询商品属性中间
     */
    @Override
    public PAttrCommonVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品属性中间列表
     */
    @Override
    public TableDataInfo<PAttrCommonVo> queryPageList(PAttrCommonBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PAttrCommon> lqw = buildQueryWrapper(bo);
        Page<PAttrCommonVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品属性中间列表
     */
    @Override
    public List<PAttrCommonVo> queryList(PAttrCommonBo bo) {
        LambdaQueryWrapper<PAttrCommon> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PAttrCommon> buildQueryWrapper(PAttrCommonBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PAttrCommon> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStrKey()), PAttrCommon::getStrKey, bo.getStrKey());
        lqw.eq(StringUtils.isNotBlank(bo.getStrValue()), PAttrCommon::getStrValue, bo.getStrValue());
        lqw.eq(bo.getProductId() != null, PAttrCommon::getProductId, bo.getProductId());
        return lqw;
    }

    /**
     * 新增商品属性中间
     */
    @Override
    public Boolean insertByBo(PAttrCommonBo bo) {
        PAttrCommon add = BeanUtil.toBean(bo, PAttrCommon.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品属性中间
     */
    @Override
    public Boolean updateByBo(PAttrCommonBo bo) {
        PAttrCommon update = BeanUtil.toBean(bo, PAttrCommon.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PAttrCommon entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品属性中间
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
